Skip to content

feat(binding)!: take property paths as delegates so call sites build no expression tree - #549

Merged
glennawatson merged 1 commit into
mainfrom
feat/delegate-selectors
Nov 18, 2026
Merged

glennawatson merged 1 commit into
mainfrom
feat/delegate-selectors

Conversation

@glennawatson

Copy link
Copy Markdown
Contributor

Summary

The binding methods take each property path as a delegate, Func<TSource, TValue>, so a call site builds no expression tree.

  • WhenChanged, WhenChanging, WhenAny, WhenAnyValue, WhenAnyObservable, BindOneWay, BindTwoWay, OneWayBind, Bind, BindTo, BindCommand, BindInteraction and InvokeCommand take their path selectors as Func. The generated overloads and interceptors match.
  • The Unsafe overloads keep Expression<Func<TSource, TValue>>, because they read the tree by reflection.
  • A path may use ?.. x => x.Child?.Name is read as the path Child.Name, the same as x => x.Child!.Name.
  • New analyzer rule RXUIBIND022 reports a selector that is not a property path: a statement body, a computed value, a member of anything but the lambda's parameter, or ?. ahead of a plain value.
  • The examples, tests and benchmarks mark their selector lambdas static.

Why

An Expression<Func<...>> parameter makes the compiler build the tree at the call site on every call, through reflection, even though generated code never reads the argument. A viewer that subscribes about a hundred paths on each tab switch spent most of that switch building trees it threw away. On WhenChangedBenchmark's first observation the call drops from 3.49 us and 920 B to 1.25 us and 392 B on .NET 11, with the same result on .NET 10 and NativeAOT.

Breaking changes

  • The plain binding methods take Func<TSource, TValue> where they took Expression<Func<TSource, TValue>>. An inline lambda compiles as before. A stored expression no longer compiles against them: pass it to the Unsafe overload, or write the lambda inline.
  • The generator and the runtime package must be the same version. A generator from this version writes Func signatures and does not match an older runtime's stubs.
  • RXUIBIND022 is an error. It reports selectors that already had no generated binding and threw when run.
  • A selector lambda is a delegate to other analyzers, so rules such as "make this lambda static" now apply to it.

How this was verified

New tests cover the path reader, the ?. forms, each RXUIBIND022 reason, and a check of the compiled caller's IL that finds no call into System.Linq.Expressions, with and without interceptors. The whole ReactiveUI.SourceGenerators.slnf builds and its tests run on Linux. The Uno filter was not built here because its Windows head needs MSBuild on Windows.

Notes for the reviewer

  • Start with ExtractorValidation.IsPropertySelector and PathSyntax. A delegate type alone does not mark a path out from a result selector or a converter, so the stub's parameter name decides. A new stub's path parameters must pass that test.
  • ?. ahead of a plain value, as in x => x.Child?.Count, is not generated. It makes the lambda return int?, which is not the member's type.
  • Mechanical and safe to skim: the stub files under ReactiveUI.Binding.Shared/Mixins, the PublicAPI.txt baselines, the *.verified.cs snapshots, and the static modifiers in examples, tests and benchmarks.
  • Out of scope: read-only fields at the end of a path and static fields are still not generated.

Checklist

  • I have read the Contribute guide
  • The PR title follows Conventional Commits
  • Tests cover this change, or the summary says why they do not
  • New or changed public API has XML documentation

- Take each path selector as Func<TSource, TValue> in the binding stubs, the generated overloads and the interceptors. An expression tree parameter makes the compiler build the tree at every call site.
- Keep Expression<Func<TSource, TValue>> on the Unsafe overloads, which read the tree by reflection.
- Read a path written with ?. as the members it names.
- Add RXUIBIND022 for a selector that is not a property path.
- Mark selector lambdas static in the examples, tests and benchmarks.

BREAKING CHANGE: the plain binding methods take Func<TSource, TValue>. Pass a stored expression to the Unsafe overload.
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.01%. Comparing base (b8243d6) to head (07bd6a5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #549      +/-   ##
==========================================
- Coverage   99.01%   99.01%   -0.01%     
==========================================
  Files         471      472       +1     
  Lines       18864    18920      +56     
  Branches     2793     2813      +20     
==========================================
+ Hits        18679    18734      +55     
- Misses        185      186       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@glennawatson
glennawatson merged commit a432307 into main Nov 18, 2026
23 checks passed
@glennawatson
glennawatson deleted the feat/delegate-selectors branch November 18, 2026 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant